// pages/user_end/mine/mine.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    token: '',
    login: 0,
    userinfo: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token: res.token
          })
          that.getUserInfo();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  goLogin: function () {
    console.log('登录');
    wx.navigateTo({
      url: '../../authorization/authorization'
    })
  },
  goQuestion: function () {
    wx.navigateTo({
      url: '../question/question'
    })
  },
  goUserinfo: function () {
    if (this.data.login == 0) {
      this.goLogin();
      return false;
    }
    wx.navigateTo({
      url: '../user_info/user_info'
    })
  },
  goPoster: function () {
    if (this.data.login == 0) {
      this.goLogin();
      return false;
    }
    wx.navigateTo({
      url: '../posters/posters'
    })
  },
  goCommission: function () {
    if (this.data.login == 0) {
      this.goLogin();
      return false;
    }
    wx.navigateTo({
      url: '../commission/index/index'
    })
  },
  goOrder: function (e) {
    var checkinfo = e.currentTarget.dataset.type;
    console.log('订单列表页面');
    if (this.data.login == 0) {
      this.goLogin();
      return false;
    }
    wx.navigateTo({
      url: '../order/order?checkinfo=' + checkinfo
    })
  },
  gozjOrder: function (e) {
    var checkinfo = e.currentTarget.dataset.type;
    console.log('订单列表页面');
    if (this.data.login == 0) {
      this.goLogin();
      return false;
    }
    wx.navigateTo({
      url: '../zjorder/zjorder?checkinfo=' + checkinfo
    })
  },
  gopxOrder: function (e) {
    var checkinfo = e.currentTarget.dataset.type;
    console.log('订单列表页面');
    if (this.data.login == 0) {
      this.goLogin();
      return false;
    }
    wx.navigateTo({
      url: '../pxorder/pxorder?checkinfo=' + checkinfo
    })
  },
  
  gozjOrder: function (e) {
    var checkinfo = e.currentTarget.dataset.type;
    console.log('订单列表页面');
    if (this.data.login == 0) {
      this.goLogin();
      return false;
    }
    wx.navigateTo({
      url: '../zjorder/zjorder?checkinfo=' + checkinfo
    })
  },
  
  goChange: function () {
    if (this.data.login == 0) {
      this.goLogin();
      return false;
    }
    wx.navigateTo({
      url: '../../select_identity/select_identity'
    })
  },
  loginout:function(){
	  cookie.setCookie('userInfo', null);  
	wx.showToast({
	  title:'退出成功!',
	  icon:'none'
	});	
	 this.goLogin();
  },
  goCollect: function () {
    if (this.data.login == 0) {
      this.goLogin();
      return false;
    }
    wx.navigateTo({
      url: '../user_collection/user_collection'
    })
  },
  goAddress: function () {
    console.log('用户地址页面');
    if (this.data.login == 0) {
      this.goLogin();
      return false;
    }
    wx.navigateTo({
      url: '../address_list/address_list'
    })
  },
  goContractList: function () {
    console.log('用户合同');
    if (this.data.login == 0) {
      this.goLogin();
      return false;
    }
    wx.navigateTo({
      url: '../contract_list/contract_list'
    })
  },
  goVip: function() {
    if (this.data.login == 0) {
      this.goLogin();
      return false;
    }
    wx.navigateTo({
      url: '../../vip/vip'
    })
  },
  getUserInfo: function () {
    var that = this;
    let param = {
      scoure: 'wxapp',
      token: that.data.token
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.getuserinfo, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if (res.data.code == 1) {
        that.setData({
          login: 1,
          userinfo: res.data.data
        })
      }
    })
  }
})